<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./idb.js"></script>
</head>

<body>
    <script>
        const NAME = 'person';
        let db;
        let request = window.indexedDB.open(NAME);

        request.onerror = function (event) {
            console.log('数据库打开报错');
        };

        request.onsuccess = function (event) {
            db = request.result;
            console.log(112)
            // read()
            readbyCursor()
            // add()
            console.log('数据库打开成功');
        };

        request.onupgradeneeded = function (event) {
            db = event.target.result;
            var objectStore;
            // if (!db.objectStoreNames.contains(NAME)) {
                objectStore = db.createObjectStore(NAME, {
                    autoIncrement: true
                });
                objectStore.createIndex('csadqwe', 'qweqweqw', {
                    unique: false
                });
                objectStore.createIndex('email', 'email', {
                    unique: false
                });
                objectStore.createIndex('typec', 'typec', {
                    unique: false
                });
                console.log('索引完了')
            // }

        }

        function add() {
            var request = db.transaction([NAME], 'readwrite')
                .objectStore(NAME)
                .add({
                    name: '赵六',
                    // age: 25,
                    email: 'zhaoliu@example.com',
                    type:'text'
                });

            request.onsuccess = function (event) {
                console.log('数据写入成功');
            };

            request.onerror = function (event) {
                console.log(event.target.error)
                console.log('数据写入失败');
            }
        }


        function read() {
            var transaction = db.transaction([NAME], 'readonly');
            var store = transaction.objectStore(NAME);
            // console.log(objectStore)
            // var request = objectStore.get(1);
            var index = store.index('type');
            var request = index.getAll('text');
            request.onerror = function (event) {
                console.log('事务失败');
            };

            request.onsuccess = function (event) {
                console.log(request)
                if (request.result) {
                    request.result.forEach(res => {
                        console.log('Name: ' + res.name);
                        console.log('type: ' + res.type);
                        console.log('Email: ' + res.email);
                    });
                    
                } else {
                    console.log('未获得数据记录');
                }
            };
        }

        function readbyCursor() {
            var transaction = db.transaction([NAME], 'readonly');
            var store = transaction.objectStore(NAME);
            // console.log(objectStore)
            // var request = objectStore.get(1);
            var cursor = store.openCursor();
            cursor.onerror = function (event) {
                console.log('事务失败');
            };

            cursor.onsuccess = function(event) {
                var c = event.target.result;
                if (c && c.value.type==='log') {
                    // console.log(c.value)
                    top.postMessage(c.value)
                    c.continue();
                }
            };
        }

        window.addEventListener('message',function(ev,data){
            console.log(ev)
        })
    </script>
</body>

</html>